package twistter;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class IHM extends JFrame {
		
	private Container cPanel;
	private JTextField txtInputTags;
	private JPanel pnlTweets;
	private JPanel pnlFavoritesTweets;
	private List<TweetPanel> tweets;

	public IHM(int xSize, int ySize) throws IOException {
		super("Twistter");
		this.init(xSize, ySize);
	}
	
	private void init(int xSize, int ySize) throws IOException {
		
		WindowListener l = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		};
		this.addWindowListener(l);
		this.tweets = new ArrayList<TweetPanel>();
		
		this.cPanel = this.getContentPane();
		this.txtInputTags = new JTextField();
		
		// Top :
		Box boxTop = new Box(BoxLayout.LINE_AXIS);
		JLabel lblTags = new JLabel("Tags : ");
		boxTop.add(lblTags);
		this.txtInputTags.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				onChange();
			}
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				onChange();
			}
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				onChange();
			}
			public void onChange() {
				filterTweets(txtInputTags.getText());
			}
		});
		boxTop.add(this.txtInputTags);
		this.cPanel.add(boxTop, BorderLayout.PAGE_START);
		
		// Middle :
		Box boxMiddle = new Box(BoxLayout.LINE_AXIS);
		this.pnlTweets = new JPanel();
		this.pnlTweets.setLayout(new BoxLayout(this.pnlTweets, BoxLayout.Y_AXIS));
		
		// Tweets imports :
		TweetImporter importer = new TweetImporter();
		List<Tweet> tweets = importer.importTweets();
		for ( Tweet t : tweets ) {
			//TweetPanel tweetRender = new TweetPanel("testimage.jpg","nom","contenu");
			final TweetPanel tweetRender = new TweetPanel(t);
			tweetRender.getBtnFav().addActionListener(new ActionListener() {
			    @Override
			    public void actionPerformed(ActionEvent e) {
			    	tweetRender.getTweet().setFavorite(true);
			    	pnlFavoritesTweets.add(tweetRender);
			    	revalidate();
			    }
			});
			this.tweets.add(tweetRender);
			this.pnlTweets.add(tweetRender, BorderLayout.LINE_START);
		}
		
		JScrollPane spPanel = new JScrollPane(this.pnlTweets);
		boxMiddle.add(spPanel);
		this.cPanel.add(boxMiddle,BorderLayout.CENTER);
		
		// Bottom :
		Box boxBottom = new Box(BoxLayout.LINE_AXIS);
		JLabel lblFavoritesTweets = new JLabel("Tweets favoris : ");
		boxBottom.add(lblFavoritesTweets);
		this.pnlFavoritesTweets = new JPanel();
		this.pnlFavoritesTweets.setLayout(new BoxLayout(this.pnlFavoritesTweets, BoxLayout.Y_AXIS));
		JScrollPane spPanel2 = new JScrollPane(this.pnlFavoritesTweets);
		boxBottom.add(spPanel2);
		//boxBottom.add(this.pnlFavoritesTweets);
		boxBottom.setPreferredSize(new Dimension(1000,200));
		this.cPanel.add(boxBottom,BorderLayout.PAGE_END);
		
		this.setContentPane(this.cPanel);
		this.setSize(xSize, ySize);
		this.setMinimumSize(new Dimension(xSize, ySize));
		this.setLocation(500, 50);
		
	}

	public void showIHM() {
		this.setVisible(true);
	}
	
	public void filterTweets(String filterString) {
		for ( TweetPanel t : this.tweets ) {
			t.setVisible(t.getTweet().getContent().contains(filterString));
		}
	}
	
	public String ToString() {
		return "IHM";
	}

}